package swagger.model;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;

import com.alibaba.fastjson.JSON;

import common.model.ResponseObject;
import common.util.BeanUtil;

@SuppressWarnings("all")
public abstract class SwaggerModelObject {
	
	protected abstract Object getId();
	
	protected Map<String, Object> format(){
		return BeanUtil.parseBean(this);

	}
	
	public ResponseObject format(Object error){
		ResponseObject res = new ResponseObject();
	     
	    res.setData(format());
	    res.setCode(null == error ? 0 : 1);
	    if (null != error){	
	    	res.setError(error);
	    }
	    
	    return res;
	}
	
	public static <T extends SwaggerModelObject> ResponseObject format(List<T> models, Object error){
		ResponseObject res = new ResponseObject();
	     
		List<Map<String, Object>> items = new ArrayList<Map<String, Object>>();
		for(SwaggerModelObject model : models){
			items.add(model.format());
		}
		
	    res.setData(items);
	    res.setCode(null == error ? 0 : 1);
	    if (null != error){
	    	res.setError(error);
	    }
	    
	    return res;
	}
	
	public static ResponseObject fromList(List<Map<String, Object>> list, Object error){
		ResponseObject res = new ResponseObject();
	   
	    res.setData(list);
	    
	    res.setCode(null == error ? 0 : 1);
	    if (null != error){
	    	res.setError(error);
	    }
	    
	    return res;
	}
	
	@Override
	public int hashCode() {
		return getId().hashCode();
	}

	@Override
	public boolean equals(Object obj) {
		return null == obj ? null == this : obj.hashCode() == hashCode();
	}

	@Override
	public String toString() {
		return format().toString();
	}
}
